package com.hmall.order.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmall.common.client.ItemClient;
import com.hmall.common.client.UserClient;
import com.hmall.common.pojo.Address;
import com.hmall.common.pojo.Item;
import com.hmall.order.mapper.OrderDetailMapper;
import com.hmall.order.mapper.OrderLogisticsMapper;
import com.hmall.order.mapper.OrderMapper;
import com.hmall.order.pojo.DownOrder;
import com.hmall.order.pojo.Order;
import com.hmall.order.pojo.OrderDetail;
import com.hmall.order.pojo.OrderLogistics;
import com.hmall.order.service.IOrderService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
@GlobalTransactional
public class OrderService extends ServiceImpl<OrderMapper, Order> implements IOrderService {

    @Autowired(required = false)
    private ItemClient itemClient;

    @Autowired(required = false)
    private UserClient userClient;

    @Autowired(required = false)
    private OrderMapper orderMapper;

    @Autowired(required = false)
    private OrderDetailMapper orderDetailMapper;

    @Autowired(required = false)
    private OrderLogisticsMapper orderLogisticsMapper;



    @Override
    public long order(DownOrder order) {

         //2）商品微服务提供FeignClient，实现根据id查询商品的接口
        //根据地址id查询用户id
        Address address = userClient.getAddressById(order.getAddressId());
        //3）根据itemId查询商品信息
        Item item = itemClient.findById(order.getItemId());
         //4）基于商品价格、购买数量计算商品总价：totalFee
        Long price = item.getPrice();
        Integer num = order.getNum();
        Long totalFee = price*num;
         //5）封装Order对象，初识status为未支付
        Order order1 = new Order();
        order1.setTotalFee(totalFee);
        order1.setPaymentType(order.getPaymentType());
        order1.setUserId(address.getUserId());
        order1.setStatus(1);
        order1.setCreateTime(new Date());
        order1.setPayTime(null);
        order1.setConsignTime(null);
        order1.setEndTime(null);
        order1.setCloseTime(null);
        order1.setCommentTime(null);
        order1.setUpdateTime(null);
         //6）将Order写入数据库tb_order表中
        orderMapper.insert(order1);
         //7）将商品信息、orderId信息封装为OrderDetail对象，写入tb_order_detail表
        OrderDetail orderDetail = new OrderDetail();
        orderDetail.setOrderId(order1.getId());
        orderDetail.setItemId(item.getId());
        orderDetail.setNum(order.getNum());
        orderDetail.setName("好东西");
        orderDetail.setPrice(item.getPrice());
        orderDetail.setSpec(item.getSpec());
        orderDetail.setImage(item.getImage());
        orderDetail.setCreateTime(new Date());
        orderDetail.setUpdateTime(new Date());

        orderDetailMapper.insert(orderDetail);

        //8）将user-service的根据id查询地址接口封装为FeignClient
         //9）根据addressId查询user-service服务，获取地址信息
         //10）将地址封装为OrderLogistics对象，写入tb_order_logistics表
        OrderLogistics orderLogistics = new OrderLogistics();
        orderLogistics.setOrderId(order1.getId());
        orderLogistics.setLogisticsNumber(null);
        orderLogistics.setLogisticsCompany(null);
        orderLogistics.setContact(address.getContact());
        orderLogistics.setMobile(address.getMobile());
        orderLogistics.setProvince(address.getProvince());
        orderLogistics.setCity(address.getCity());
        orderLogistics.setTown(address.getTown());
        orderLogistics.setStreet(address.getStreet());
        orderLogistics.setCreateTime(new Date());
        orderLogistics.setUpdateTime(new Date());

        orderLogisticsMapper.insert(orderLogistics);

        //11）在item-service提供减库存接口，并编写FeignClient
         //12）调用item-service的减库存接口
        Integer numb = item.getStock()-order.getNum();

        itemClient.deductItemStock(item.getId(),numb);

        return order1.getId();
    }
}
